package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommentBean;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.beans.PhotoDetailBean;
import com.dtw.ditouwang.databinding.ActivityEditorChoosePhotoDetailBinding;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.PhotoPreviewActivity;
import com.dtw.ditouwang.ui.home.ImagePeopleAdapter;
import com.dtw.ditouwang.utils.ShareUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.wechat.Wechat;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditorChoosePhotoDetailActivity extends BaseActivity implements OnItemClickListener, View.OnClickListener, TextView.OnEditorActionListener {
    public static final String KeyID = "KeyID";
    TextView awesome, collection;
    ActivityEditorChoosePhotoDetailBinding activityEditorChoosePhotoDetailBinding;
    RecyclerView recyclerView, recyclerviewComment;
    ImagePeopleAdapter imagePeopleAdapter;
    CommentAdapter commentAdapter;
    ArrayList<String> imgList = new ArrayList<>();
    List<CommentBean.DataBean> commentList = new ArrayList<>();
    String contextText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityEditorChoosePhotoDetailBinding = DataBindingUtil.setContentView(this, R.layout.activity_editor_choose_photo_detail);
        enableBack();
        setTitle("详情");
        setSubTitle("举报", this);
        awesome = findViewById(R.id.text_awesome);
        collection = findViewById(R.id.text_collection);
        recyclerView = findViewById(R.id.recyclerview);
        recyclerviewComment = findViewById(R.id.recycler_commend);
        findViewById(R.id.sendButton).setOnClickListener(this);
        ((EditText) findViewById(R.id.text_send_comment)).setOnEditorActionListener(this);
        recyclerviewComment.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        imagePeopleAdapter = new ImagePeopleAdapter(imgList);
        imagePeopleAdapter.setOnItemClickListener(this);
        commentAdapter = new CommentAdapter(commentList);
        commentAdapter.setMoreCommentListener(this);
        recyclerView.setAdapter(imagePeopleAdapter);
        recyclerviewComment.setAdapter(commentAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        webApi.getPhotoDetail(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0)).enqueue(new Callback<PhotoDetailBean>() {
            @Override
            public void onResponse(Call<PhotoDetailBean> call, Response<PhotoDetailBean> response) {
                if (response.body().isResult() && !isFinishing()) {
                    activityEditorChoosePhotoDetailBinding.setPhoto(response.body().getData());
                    contextText=response.body().getData().getArticle_info().getA_info();
                    awesome.setSelected(response.body().getData().getArticle_info().getIs_thumbs() == 1);
                    collection.setSelected(response.body().getData().getArticle_info().getIs_collection() == 1);
                    imgList.clear();
                    if(response.body().getData().getArticle_info().getImg()!=null) {
                        imgList.addAll(response.body().getData().getArticle_info().getImg());
                    }
                    imagePeopleAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<PhotoDetailBean> call, Throwable t) {

            }
        });
        getComment(1);
    }

    private void getComment(int order) {
        webApi.getComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), order).enqueue(new Callback<CommentBean>() {
            @Override
            public void onResponse(Call<CommentBean> call, Response<CommentBean> response) {
                if (response.body().isResult()) {
                    commentList.clear();
                    commentList.addAll(response.body().getData());
                    commentAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<CommentBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int viewID, int position, int state) {
        switch (viewID) {
            case R.id.text_comment_child_more:
                Intent intent = new Intent(this, CommentMoreActivity.class);
                intent.putExtra(CommentMoreActivity.KeyCommentID, commentList.get(position).getA_id());
                intent.putExtra(CommentMoreActivity.KeyArtID, getIntent().getIntExtra(KeyID, 0));
                startActivity(intent);
                break;
            case R.id.text_awesome:
                webApi.setAwesomeForComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), commentList.get(position).getA_id(), 0, state).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
            case R.id.recyclerview:
                Intent img = new Intent(this, PhotoPreviewActivity.class);
                img.putStringArrayListExtra(PhotoPreviewActivity.KeyUrlList, imgList);
                startActivity(img);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_sub_title:
                Intent intent = new Intent(this, FrequenctActivity.class);
                intent.putExtra(FrequenctActivity.KeyID, getIntent().getIntExtra(KeyID, 0));
                startActivity(intent);
                break;
            case R.id.text_send_comment:
                break;
            case R.id.view_like:
                awesome.setSelected(!awesome.isSelected());
                webApi.setAwesome(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), collection.isSelected() ? 1 : 0).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (response.body() == null) {
                            showToast("服务异常");
                            return;
                        }
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
            case R.id.view_share:
                ShareParams shareParams;
                shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setTitle("低头网");
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                shareParams.setImageData(bitmap);
                shareParams.setText(contextText==null?"":contextText);
                shareParams.setUrl("http://www.ditouwang.net/");
                ShareUtil.share(this,shareParams);
                break;
            case R.id.view_collection:
                collection.setSelected(!collection.isSelected());
                webApi.setCollection(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), collection.isSelected() ? 1 : 0).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (!response.body().isResult()) {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });

                break;
            case R.id.sendButton:
                webApi.sendComment(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), activityEditorChoosePhotoDetailBinding.textSendComment.getText().toString()).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (response.body().isResult()) {
                            showToast("评论成功");
                            onStart();
                            activityEditorChoosePhotoDetailBinding.textSendComment.setText(null);
                        } else {
                            showToast(response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
        }
    }

    @Override
    public boolean onEditorAction(final TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            findViewById(R.id.sendButton).callOnClick();
        }
        return false;
    }
}
